package com.gxgy.service.impl;


import java.nio.channels.NonWritableChannelException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.gxgy.base.BaseAllMapper;
import com.gxgy.entity.Role;
import com.gxgy.entity.User;
import com.gxgy.service.UserService;
import com.gxgy.uilt.BaseUtil;
import com.gxgy.uilt.Id;
import com.gxgy.uilt.IfVersionException;
import com.gxgy.uilt.MD5Util;

/**
 * @description: 用户
 * @author: 邓忠柠
 * @date: 2021-11-10
 */
@Service
public class UserServiceImpl extends BaseAllMapper implements UserService {
	

	/**
	 * @description: 新增user
	 * @author:邓忠柠
	 * @throws Exception 
	 * @date: 2021-11-10
	 */
	@Transactional
	@Override
	public Integer addOneUser(String name,String username,String sex,
			String password,String status,String createTime,Model model)  {
		if (name.isEmpty()) {
			throw new IfVersionException("name不能为空!");
		}
		if (username.isEmpty()) {
			model.addAttribute("msg","用户名不能为空");
			throw new IfVersionException("username不能为空!");
		}
		if (sex.isEmpty()) {
			model.addAttribute("msg","性别不能为空");
			throw new IfVersionException("性别不能为空！");
		}
		if (password.isEmpty()) {
			model.addAttribute("msg","密码不能为空");
			throw new IfVersionException("password不能为空!");
		}
		if (status.isEmpty()) {
			model.addAttribute("msg","身份不能为空");
			throw new IfVersionException("角色名称不能为空");
		}
		QueryWrapper<User> queryUserByName = new QueryWrapper<User>()
				.eq(User.USERNAME, username);
		User returnUser = userMapper.selectOne(queryUserByName);
		if (returnUser != null) {
			model.addAttribute("msg","该用户名已存在");
			throw new IfVersionException("usename已存在，该用户名已存在");
		}
		QueryWrapper<Role> queryRoleByNname = new QueryWrapper<Role>()
				.eq(Role.ROLE_NAME, status);
		Role role = roleMapper.selectOne(queryRoleByNname);
		if (role == null) {
			model.addAttribute("msg","该身份不存在");
			throw new IfVersionException("该角色不存在");
		}
		String id = BaseUtil.getBaseId();
		User user = new User();
				user.setId(Id.user_+MD5Util.md5Encryption(id));
				user.setName(name);
				user.setUsername(username);
				if (sex.equals("男")) {
					user.setSex("0");
				}
				user.setSex("1");
				user.setPassword(MD5Util.md5Encryption(password));
				user.setrId(role.getId());
				user.setCreateTime(createTime);
		return userMapper.insert(user);
		
	}

	
	/**
	 * @description: 查询user表通过用户名查询
	 * @author: 邓忠柠
	 * @date: 2021-11-10
	 */
	@Override
	public User queryUserByUserName(String username) {
		
		if (username.isEmpty()) {
			throw new IfVersionException("用户名不能为空");
		}
		QueryWrapper<User> wrapper = new QueryWrapper<User>()
				.eq(User.USERNAME, username);
		return userMapper.selectOne(wrapper);
	}


	/**
	 * @description: 查询user 查询所有
	 * @author: 邓忠柠
	 * @date: 2021-11-10
	 */
	@Override
	public List<User> queryAllUser() {
		return userMapper.selectList(null);
	}


	/**
	 * @description: 查询user表通过id查询
	 * @author: 邓忠柠
	 * @date: 2021-11-10
	 */
	@Override
	public User queryUserById(String id) {
		if (id.isEmpty()) {
			throw new IfVersionException("id不能为空");
		}
		QueryWrapper<User> wrapper = new QueryWrapper<User>()
				.eq(User.ID, id);
		User user = userMapper.selectOne(wrapper);
		if (user == null) {
			throw new IfVersionException("id不存在");
		}
		return userMapper.selectOne(wrapper);
	}


	/**
	 * @description: 删除user表通过id查询
	 * @author: 邓忠柠
	 * @date: 2021-11-10
	 */
	@Override
	public Integer deleteUserById(String id) {
		if (id.isEmpty()) {
			throw new IfVersionException("id不能为空");
		}
		QueryWrapper<User> wrapper = new QueryWrapper<User>()
				.eq(User.ID, id);
		User user = userMapper.selectOne(wrapper);
		if (user == null) {
			throw new IfVersionException("id不存在");
		}
		
		System.out.println("返回1删除成功");
		return userMapper.delete(wrapper);
	}


	/**
	 * @description: 更新修改user表 单条记录
	 * @author: 邓忠柠
	 * @date: 2021-11-10
	 */
	@Override
	public Integer updateUser(String id,String name,String username,String sex,String password,
			String status,String createTime,Model model) {
		if (name.isEmpty()) {
			throw new IfVersionException("name不能为空!");
		}
		if (username.isEmpty()) {
			model.addAttribute("msg","用户名不能为空");
			throw new IfVersionException("username不能为空!");
		}
		if (sex.isEmpty()) {
			model.addAttribute("msg","性别不能为空");
			throw new IfVersionException("性别不能为空！");
		}
		if (password.isEmpty()) {
			model.addAttribute("msg","密码不能为空");
			throw new IfVersionException("password不能为空!");
		}
		if (status.isEmpty()) {
			model.addAttribute("msg","身份不能为空");
			throw new IfVersionException("角色名称不能为空");
		}
		QueryWrapper<User> queryUserByName = new QueryWrapper<User>()
				.eq(User.USERNAME, username);
		User returnUser = userMapper.selectOne(queryUserByName);
		if (returnUser != null) {
			model.addAttribute("msg","该用户名已存在");
			throw new IfVersionException("usename已存在，该用户名已存在");
		}
		QueryWrapper<Role> queryRoleByNname = new QueryWrapper<Role>()
				.eq(Role.ROLE_NAME, status);
		Role role = roleMapper.selectOne(queryRoleByNname);
		if (role == null) {
			model.addAttribute("msg","该身份不存在");
			throw new IfVersionException("该角色不存在");
		}
		User user = new User();
				user.setId(id);
				user.setName(name);
				user.setUsername(username);
				if (sex.equals("男")) {
					user.setSex("0");
				}
				user.setSex("1");
				user.setPassword(MD5Util.md5Encryption(password));
				user.setrId(role.getId());
				user.setCreateTime(createTime);

		UpdateWrapper<User> wrapper = new UpdateWrapper<User>()
				.eq(User.ID, user.getId());
		System.out.println("返回1 修改成功");
		return userMapper.update(user,wrapper);
	}






	
	
	
	
	
	
	

}
